//package com.soujiayi.view;
//
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
//import com.soujiayi.activity.MyApplication;
//import com.soujiayi.activity.R;
//import com.soujiayi.activity.TuanDetailActivity;
//import com.soujiayi.adapter.TuanListAdapter;
//import com.soujiayi.model.TuanDetail;
//import com.soujiayi.model.TuanMain;
//import com.soujiayi.model.TuanMains;
//import com.soujiayi.net.HttpRequester;
//import com.soujiayi.net.NetworkUtils;
//import com.soujiayi.store.DatabaseManager;
//import com.soujiayi.util.Constants;
//
//import android.app.Activity;
//import android.content.Intent;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.Message;
//import android.support.v4.app.Fragment;
//import android.util.Log;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.AbsListView;
//import android.widget.AbsListView.OnScrollListener;
//import android.widget.AdapterView;
//import android.widget.AdapterView.OnItemClickListener;
//import android.widget.LinearLayout;
//import android.widget.ListView;
//import android.widget.ProgressBar;
//import android.widget.TextView;
//
//public class TuanViewPagerFragment extends Fragment implements OnScrollListener {
//
//	private Activity parentActivity;
//	private String catId;
//	private String cityName;
//	private int pst = 0;
//	private int maxSize = 0;
//	private final int initSize = 5;
//	private final int limit = 20;
//	private View viewFooter;
//	private ProgressBar pb_tuan_loadmore;
//	private TextView tv_tuan_total;
//	private TextView tv_tuan_current;
//	private TextView tv_tuan_loadmore;
//	private TuanListAdapter tuan_list_adapter;
//	private ListView lv_tuan_goods;
//	private ArrayList<TuanMain> list;
//	private String preCity;
//	private ProgressBar pb_tuan_loading;
//	private LinearLayout ll_tuan_record;
//	private LinearLayout ll_tuan_loadmore;
//	private HttpRequester httpRequester;
//	private DatabaseManager databaseManager = null;
//	private View viewList;
//	private boolean forceUpdateFlag = true;
//
//	public TuanViewPagerFragment(String catId, String cityName) {
//		this.catId = catId;
//		this.preCity = cityName;
//		this.cityName = cityName;
//	}
//
//	public void setCityName(String cityName) {
//		if (!this.cityName.equals(cityName)) {
//			this.preCity = this.cityName;
//			this.cityName = cityName;
//			forceUpdate();
//		}
//	}
//
//	public void forceUpdate() {
//		this.forceUpdateFlag = true;
//	}
//
//	@Override
//	public void onCreate(Bundle savedInstanceState) {
//		// TODO Auto-generated method stub
//		super.onCreate(savedInstanceState);
//		this.parentActivity = getActivity();
//		databaseManager = new DatabaseManager(parentActivity);
//		initAdapter();
//		httpRequester = new HttpRequester();
//	}
//
//	@Override
//	public View onCreateView(LayoutInflater inflater, ViewGroup container,
//			Bundle savedInstanceState) {
//		// TODO Auto-generated method stub
//		if (container == null)
//			return null;
//		viewList = inflater.inflate(R.layout.tuan_list, container, false);
//		ll_tuan_record = (LinearLayout) viewList
//				.findViewById(R.id.ll_tuan_record);
//		tv_tuan_total = (TextView) viewList.findViewById(R.id.tv_tuan_total);
//		tv_tuan_current = (TextView) viewList
//				.findViewById(R.id.tv_tuan_current);
//		pb_tuan_loading = (ProgressBar) viewList
//				.findViewById(R.id.pb_tuan_loading);
//		lv_tuan_goods = (ListView) viewList.findViewById(R.id.lv_tuan_goods);
//		viewFooter = inflater.inflate(R.layout.tuan_load_more, null);
//		ll_tuan_loadmore = (LinearLayout) viewFooter
//				.findViewById(R.id.ll_tuan_loadmore);
//		tv_tuan_loadmore = (TextView) viewFooter
//				.findViewById(R.id.tv_tuan_loadmore);
//		pb_tuan_loadmore = (ProgressBar) viewFooter
//				.findViewById(R.id.pb_tuan_loadmore);
//		lv_tuan_goods.addFooterView(viewFooter);
//		return viewList;
//	}
//
//	@Override
//	public void onActivityCreated(Bundle savedInstanceState) {
//		// TODO Auto-generated method stub
//		super.onActivityCreated(savedInstanceState);
//
//		if (forceUpdateFlag) {
//			pb_tuan_loading.setVisibility(View.VISIBLE);
//			lv_tuan_goods.setVisibility(View.GONE);
//			ll_tuan_record.setVisibility(View.GONE);
//
//			new Thread(new Runnable() {
//				@Override
//				public void run() {
//					reloadData();
//					handler.sendEmptyMessage(Constants.TUAN_INIT_LOAD);
//				}
//			}).start();
//		} else {
//			lv_tuan_goods.setAdapter(tuan_list_adapter);
//		}
//
//		lv_tuan_goods.setOnScrollListener(this);
//		lv_tuan_goods.setOnItemClickListener(new OnItemClickListener() {
//			@Override
//			public void onItemClick(AdapterView<?> arg0, View arg1, int i,
//					long arg3) {
//				Intent intent = new Intent(parentActivity,
//						TuanDetailActivity.class);
//				intent.putExtra(Constants.TUAN_MAIN, list.get(i));
//				intent.putExtra(Constants.TUAN_CITY, cityName);
//				TuanViewPagerFragment.this.startActivity(intent);
//				parentActivity.overridePendingTransition(R.anim.push_right_in,
//						R.anim.push_right_out);
//			}
//		});
//		ll_tuan_loadmore.setOnClickListener(new View.OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				tv_tuan_loadmore.setVisibility(View.GONE);
//				pb_tuan_loadmore.setVisibility(View.VISIBLE);
//				new Thread(new Runnable() {
//					@Override
//					public void run() {
//						loadMoreData();
//						handler.sendEmptyMessage(Constants.TUAN_LOAD_MORE);
//					}
//				}).start();
//			}
//
//		});
//		this.forceUpdateFlag = false;
//		this.preCity = this.cityName;
//		tv_tuan_total.setText(this
//				.getString(R.string.tuan_tv_total).replace("?",
//						String.valueOf(maxSize)));
//		
//	}
//
//	private void initAdapter() {
//		list = new ArrayList<TuanMain>();
//		tuan_list_adapter = new TuanListAdapter(parentActivity, list);
//	}
//
//	public void reloadData() {
//		try {
//			list.clear();
//			pst = 0;
//			ArrayList<TuanMain> tuanMainlist = new ArrayList<TuanMain>();
//			TuanMains tuanMains = new TuanMains();
//			tuanMains = searchTuan(pst, initSize);
//			if (tuanMains != null) {
//				tuanMainlist = tuanMains.getTuanMainList();
//				// String total = tuanMains.getTotal();
//				String total_found = tuanMains.getTotal_found();
//				if (!"".equals(total_found) && total_found != null) {
//					maxSize = Integer.valueOf(total_found);
//					for (int i = 0; i < tuanMainlist.size(); i++) {
//						list.add(tuanMainlist.get(i));
//					}
//					tv_tuan_total.setText(this
//							.getString(R.string.tuan_tv_total).replace("?",
//									total_found));
//					pst = pst + initSize;
//				} else
//					tv_tuan_total.setText(this
//							.getString(R.string.tuan_tv_total)
//							.replace("?", "0"));
//			} else
//				tv_tuan_total.setText(this.getString(R.string.tuan_tv_total)
//						.replace("?", "0"));
//		} catch (Exception e) {
//			tv_tuan_total.setText(this.getString(R.string.tuan_tv_total)
//					.replace("?", "0"));
//		}
//	}
//
//	public void loadMoreData() {
//		try {
//			ArrayList<TuanMain> tuanMainlist = new ArrayList<TuanMain>();
//
//			TuanMains tuanMains = new TuanMains();
//			tuanMains = searchTuan(pst, limit);
//
//			if (tuanMains != null) {
//				tuanMainlist = tuanMains.getTuanMainList();
//				if (tuanMainlist != null && tuanMainlist.size() != 0) {
//					for (int i = 0; i < tuanMainlist.size(); i++) {
//						list.add(tuanMainlist.get(i));
//					}
//					pst = pst + limit;
//				} else
//					lv_tuan_goods.removeFooterView(viewFooter);
//			}
//		} catch (Exception e) {
//
//		}
//
//	}
//
//	public TuanMains searchTuan(int index, int stepSize) {
//		Map<String, String> parameters = new HashMap<String, String>();
//		
//		// parameters.put("q", "");
//		// parameters.put("orderby", "");
//		// parameters.put("pricearea","");
//		// parameters.put("istoday", "");
//		String cityId = databaseManager.getCityIdByName(cityName);
//		if (cityId==null || "".equals(cityId))
//			cityId = "3";
//		parameters.put("cityid", cityId);		
//		parameters.put("catid", catId);
//		parameters.put("sid", ((MyApplication) parentActivity.getApplication())
//				.getSessionId());
//		parameters.put("pst", String.valueOf(index));
//		parameters.put("limit", String.valueOf(stepSize));
//		return httpRequester.searchTuan(parameters);
//	}
//
//	@Override
//	public void onScroll(AbsListView view, int firstVisibleItem,
//			int visibleItemCount, int totalItemCount) {
//		if (list == null || list.size() == 0)
//			tv_tuan_current.setText(this.getString(R.string.tuan_tv_current)
//					.replace("?", "0"));
//		else
//			tv_tuan_current.setText(this.getString(R.string.tuan_tv_current)
//					.replace("?", String.valueOf((firstVisibleItem + 1))));
//		if (totalItemCount == maxSize + 1) {
//			lv_tuan_goods.removeFooterView(viewFooter);
//		}
//	}
//
//	@Override
//	public void onScrollStateChanged(AbsListView view, int scrollState) {
//
//	}
//
//	private Handler handler = new Handler() {
//
//		@Override
//		public void handleMessage(Message msg) {
//			// TODO Auto-generated method stub
//			super.handleMessage(msg);
//			if (msg.what == Constants.TUAN_INIT_LOAD) {
//				pb_tuan_loading.setVisibility(View.GONE);
//				lv_tuan_goods.setVisibility(View.VISIBLE);
//				ll_tuan_record.setVisibility(View.VISIBLE);
//				lv_tuan_goods.setAdapter(tuan_list_adapter);
//				if (list == null || list.size() == 0)
//					lv_tuan_goods.removeFooterView(viewFooter);
//			} else if (msg.what == Constants.TUAN_LOAD_MORE) {
//				tv_tuan_loadmore.setVisibility(View.VISIBLE);
//				pb_tuan_loadmore.setVisibility(View.GONE);
//				tuan_list_adapter.notifyDataSetChanged();
//			}
//
//		}
//	};
//}
